package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.FenleiCaipu;
import cn.hellochaos.generator.entity.vo.FenLeiVo;
import cn.hellochaos.generator.entity.vo.FenLeiVo2;
import cn.hellochaos.generator.mapper.FenleiCaipuMapper;
import cn.hellochaos.generator.service.FenleiCaipuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* <p>
* 菜谱分类标签 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class FenleiCaipuServiceImpl extends ServiceImpl<FenleiCaipuMapper, FenleiCaipu> implements FenleiCaipuService {


    @Autowired
    private FenleiCaipuMapper fenleiCaipuMapper;

    @Override
    public List<FenLeiVo2> getCaipuFenLei() {

        List<FenLeiVo> fenlei = fenleiCaipuMapper.getFenlei();
        Iterator<FenLeiVo> iterator = fenlei.iterator();

        List<FenLeiVo2> fenLeiVo2s=new ArrayList<FenLeiVo2>();

        while (iterator.hasNext()) {
            FenLeiVo2 fenLeiVo2 = new FenLeiVo2();

            FenLeiVo fenLeiVo = (FenLeiVo) iterator.next();
            fenLeiVo2.setCategory(fenLeiVo.getCategory());
            List<String> categoryInfo=Arrays.asList(fenLeiVo.getCategoryInfo().split(","));
            fenLeiVo2.setCategoryInfo(categoryInfo);
            fenLeiVo2s.add(fenLeiVo2);
}

        System.out.println();
        return fenLeiVo2s;

    }

    @Override
    public List<CaipuList> getCaipuByFenlei(String category) {


        return fenleiCaipuMapper.getCaipuListByCategory(category);
    }

    // ---------------------------------------------------------------------------------------------
    @Override
    public Page<FenleiCaipu> listFenleiCaipusByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询fenleiCaipu: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<FenleiCaipu> queryWrapper =  new QueryWrapper<FenleiCaipu>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<FenleiCaipu> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询fenleiCaipu完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public FenleiCaipu getFenleiCaipuById(int id) {
        log.info("正在查询fenleiCaipu中id为{}的数据",id);
        FenleiCaipu fenleiCaipu = super.getById(id);
        log.info("查询id为{}的fenleiCaipu{}",id,(null == fenleiCaipu?"无结果":"成功"));
        return fenleiCaipu;
    }

    @Override
    public int insertFenleiCaipu(FenleiCaipu fenleiCaipu) {
        log.info("正在插入fenleiCaipu");
        if (super.save(fenleiCaipu)) {
            log.info("插入fenleiCaipu成功,id为{}",fenleiCaipu.getFenleiCaipuId());
            return fenleiCaipu.getFenleiCaipuId();
        } else {
            log.error("插入fenleiCaipu失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteFenleiCaipuById(int id) {
        log.info("正在删除id为{}的fenleiCaipu",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的fenleiCaipu成功",id);
            return id;
        } else {
            log.error("删除id为{}的fenleiCaipu失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateFenleiCaipu(FenleiCaipu fenleiCaipu) {
        log.info("正在更新id为{}的fenleiCaipu",fenleiCaipu.getFenleiCaipuId());
        if (super.updateById(fenleiCaipu)) {
            log.info("更新d为{}的fenleiCaipu成功",fenleiCaipu.getFenleiCaipuId());
            return fenleiCaipu.getFenleiCaipuId();
        } else {
            log.error("更新id为{}的fenleiCaipu失败",fenleiCaipu.getFenleiCaipuId());
            throw new BizException("更新失败[id=" + fenleiCaipu.getFenleiCaipuId() + "]");
        }
    }



}
